<?php
	namespace Common\Logic;
	use Common\Model\RegionsModel;
	class RegionsLogic extends RegionsModel{
		var $errorMsg = '';
		/*
		*获取地址列表
		*/
		public function getAddressList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getAddressFilter($filter);
			return $this->where($conditon)->order('region_id')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* 获取地址的搜索过滤项
		*/
		public function getAddressFilter($filter = ''){
			$conditon = array();
			if(!empty($filter['local_name'])){
				$conditon['local_name'] = array('like',$filter['local_name'].'%');
			}
			if(!empty($filter['status'])){
				$conditon['status'] = $filter['status'];
			}
			return $conditon;
		}
		
		/*
		* 获取地址总数
		*/
		public function getAddressCount($filter = ''){
			$conditon = $this->getAddressFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		*添加地址
		*/
		public function addAddress($data){
			return $this->add($data);
		}
		
		/*
		*获取区域列表
		*/
		public function getAddressAllList(){
			return $this->where(array('status'=>1))->select();
		}
		
		/*
		*获取地址——通过ID
		*/
		public function getAddressInfo($id){
			return $this->where(array('region_id'=>$id))->find();
		}
		
		/*
		*更新地址
		*/
		public function editAddress($data){
			return $this->where(array('region_id'=>$data['region_id']))->save($data);
		}
		
		/*
		* Function: checkAddress
		* Description:检查地址省市区
		* @param:$province string 省份 $city string 城市 $district 区 
		*/
		public function checkAddress($province,$city,$district){
			$provinceInfo = array();
			$cityInfo = array();
			$districtInfo = array();
			$region_id = 0;
			if(!empty($province)){
				$province = quotemeta(htmlspecialchars(trim($province)));
				if(!$provinceInfo = $this->where(array('local_name'=>array('like',$province.'%'),'status'=>1,'region_grade'=>1))->find()){
					$this->errorMsg = '不存在此省份:'.$province;
					return false;
				}else{
					if(!empty($city)){
						$city = quotemeta(htmlspecialchars(trim($city)));
						if(!$cityInfo = $this->where(array('local_name'=>$city,'status'=>1,'region_grade'=>2,'p_region_id'=>$provinceInfo['region_id']))->find()){
							$this->errorMsg = '省份:'.$province.',不存在此城市:'.$city;
							return false;
						}else{
							if(!empty($district)){
								$district = quotemeta(htmlspecialchars(trim($district)));
								if($district==$city){
									return $province.'/'.$city.':'.$cityInfo['region_id'];
								}else{
									if(!$districtInfo = $this->where(array('local_name'=>$district,'status'=>1,'region_grade'=>3,'p_region_id'=>$cityInfo['region_id']))->find()){
										$this->errorMsg = '省份:'.$province.',城市:'.$city.',不存在此地区:'.$district;
										return false;
									}else{
										return $province.'/'.$city.'/'.$district.':'.$districtInfo['region_id'];
									}
								}
							}else{
								$this->errorMsg = '地区不能为空';
								return false;
							}
						}
					}else{
						$this->errorMsg = '城市不能为空';
						return false;
					}
				}
			}else{
				$this->errorMsg = '省份不能为空';
				return false;
			}
		}
		
		/*
		* Function: queryRegionInfo
		* Description:查询地区信息
		* @param:$p_region_id string 所属级的id
		*/
		public function queryRegionInfo($p_region_id){
			$filter['p_region_id'] = $p_region_id;
			$filter['status'] = 1;
			return $this->where($filter)->select();
		}
		
		/*
		* Function: getRegionIdInfo
		* Description:查询地址等级id
		*/
		public function getRegionIdInfo($iid = ''){
			$arr = array();
			$addressIdInfo = $this->getAddressInfo($iid);
			$arr['city_id'] = $addressIdInfo['p_region_id'];
			$addressIdInfo = $this->getAddressInfo($addressIdInfo['p_region_id']);
			$arr['province_id'] = $addressIdInfo['p_region_id'];
			$arr['districtList_id'] = $iid;
			return $arr;
		}
		
		/*
		* Function: getRegionInfoByName
		* Description:获取此地区信息 by local_name
		* @param:$local_name string 地区名称
		*/
		public function getRegionInfoByName($local_name = ''){
			$filter['local_name'] = $local_name;
			$filter['status'] = 1;
			return $this->where($filter)->find();
		}
		
		/*
		* Function: getRegionInfoByNameP
		* Description:获取此地区信息 by local_name p_region_id
		* @param:$local_name string 地区名称
		*/
		public function getRegionInfoByNameP($local_name = '',$p_region_id = 0,$flag = false){
			$filter['local_name'] = $local_name;
			$filter['p_region_id'] = $p_region_id;
			if(empty($flag)){
				$filter['status'] = 1;
			}
			return $this->where($filter)->find();
		}
		
		/*
		* Function: getRegionIdDesc
		* Description:获取地址最后一条
		*/
		public function getRegionIdDesc(){
			return $this->order('region_id desc')->limit(1)->getField('region_id');
		}
		
		/*
		* Function: get_error
		* Description:获取错误信息errorMsg
		*/
		public function get_error(){
			return $this->errorMsg;
		}
	}
?>